String.prototype.trim = function trim(){
    var l = 0;
    var r = this.length -1;
    while(l < this.length)
    {   
        if(this.length >= l + 6 && this.substr(l, 6) == "&nbsp;")
        {
            l += 6;
            continue;
        }
        if(!isWhiteSpace(this.charAt(l)))
            break;
        l++;
    }
    while(r > l)
    {     
        if(r >= 6 && this.substr(r - 6, 6) == "&nbsp;")
        {
            r -= 6;
            continue;
        }
        if(!isWhiteSpace(this.charAt(r)))
            break;
        r-=1;
    }
    return this.substring(l, r+1);
};
String.format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ),
            arguments[ token + 1 ] );
    }
    return text;
};
function isWhiteSpace(_char)
{
    switch(_char){
        case ' ':
        case '\r':
        case '\n':
            return true;
        default:
            return false;
    }
}
function addDelimitation(n, remove0){
    if(n == null)
        return "";
    var res = "";
    var count = 0;
    if(n.length == null)
        n = n.toString();

    if(n.length == 0)
        return n;
    if (n.charAt(0) == '-') {
        return '-' + addDelimitation(n.substring(1));
    }
    index = 0;
    if(remove0){
        for(; index < n.length; index++){
            if(n.charAt(index) != '0')
                break;
        }
        if(index > 0){
            if(n.charAt(index) == '.')
                index--;
            if(index == 0)
                index++;
            n = n.substring(index - 1);
        }
    }
    if(n.length < 3)
        return n;
    index = n.indexOf('.');
    if(index >= 0){
        s1 = n.substring(0, index);
        var i = n.length - 1;
        for(;i > index; i--)
            if(n.charAt(i) != '0')
                break;

        s2 = n.substring(index + 1, i + 1);
        return addDelimitation(s1)+ "," + addDelimitation(s2);
    }

    for(i = n.length - 1; i >= 0; i--){
        if(n.charAt(i) < '0' || n.charAt(i) > '9'){
            count = 0;
        }
        res = n.charAt(i) + res;
        count++;
        if(count == 3 && i > 0){
            res = "." + res;
            count = 0;
        }
    }
    return res;
}

function removeDelimitation2Int(n){
    if(!n)
        return 0;
    if(n.length == null)
        return n;
    return parseInt(n.trim().replace(/\./gi, "").replace(/\,/gi, "."), 10);
}
function removeDelimitation2Float(n){
    if(!n)
        return 0;
    if(n.length == null)
        return n;
    n = n.replace(/\./g, "").replace(/\,/g, ".");
    try{
        if(n.match(/\.+/g).length > 1)
            return 0;
    }catch(err){
        return parseFloat(n);
    }
    return parseFloat(n);
}

function removeDelimitation(n){
    if(!n)
        return "";
    if(n.length == null)
        return n.toString();
    return n.replace(/\./gi, "").replace(/\,/gi, ".");
}

function isDigit(c){
    if(!c)
        return false;
    if((c < 48 || c > 57) && (c < 96 || c > 105))
        return false;
    return true;
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
    if(!rlength)
        rlength = 3;
    var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
    return newnumber; // Output the result to the form field (change for your purposes)
}